{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6db03e59",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import xlwings as xw"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e079d4e9",
   "metadata": {},
   "source": [
    "### 知识点：用numpy.random生成全排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1e145eec",
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = np.random.permutation(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5185ff0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 0, 2, 1, 5, 6, 7, 3, 4, 8])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cf630f29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10,  1,  3,  2,  6,  7,  8,  4,  5,  9])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认从0开始，转换成从1开始\n",
    "numbers + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a94ca248",
   "metadata": {},
   "source": [
    "### 启动Excel的Xlwings App"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "79c3c844",
   "metadata": {},
   "outputs": [],
   "source": [
    "app = xw.App(visible=False, add_book=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cd521599",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook = app.books.open(\"教师资格考试汇总表.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5f2818ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "sheet = workbook.sheets[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dbb0d98",
   "metadata": {},
   "source": [
    "### 计算学生个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0c846893",
   "metadata": {},
   "outputs": [],
   "source": [
    "student_number = sheet.range(\"A4\").expand('table').shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ed0dca7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student_number"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88fe3e30",
   "metadata": {},
   "source": [
    "### 生成随机全排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d64ce381",
   "metadata": {},
   "outputs": [],
   "source": [
    "student_numbers = np.random.permutation(student_number) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b82f8945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 16, 18,  6,  2, 13, 14, 12, 11,  8,  7, 15,  1, 19, 17,  5,  4,\n",
       "        3, 10,  9])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student_numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9f0abbf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "sheet.range(\"E4\").options(transpose=True).value = student_numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34b2872a",
   "metadata": {},
   "source": [
    "### 保存工作表，关闭APP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f51f414c",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook.save()\n",
    "app.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "557f5a9b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
